package com.cqeec.view;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;

import com.cqeec.domain.Student;
import com.cqeec.domain.Teacher;
import com.cqeec.service.CallRollService;
import com.cqeec.service.TeacherService;
import com.cqeec.service.imp.CallRollServiceImpl;
import com.cqeec.service.imp.TeacherServiceImpl;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.List;
import java.awt.event.ActionEvent;

/**
 * 点名界面
 * @author 邓学强
 *
 */
public class Denominate extends JInternalFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textField;
	private Teacher teacher;
	private Connection conn;
	private static List<Student> studentList ;
	private static JLabel studentname;
	private static int index;
	/**
	 * Create the frame.
	 */
	public Denominate(Connection conn, Teacher teacher) {
		setIconifiable(true);
		setClosable(true);
		this.teacher = teacher;
		setTitle("点名系统");
		setBounds(100, 100, 842, 555);
		
		textField = new JTextField();
		textField.setColumns(10);
		
		JLabel label = new JLabel("课程号：");
		label.setFont(new Font("宋体", Font.BOLD, 20));
		
		JButton button = new JButton("导入学生");
		button.addActionListener(new ActionListener() {
			

			public void actionPerformed(ActionEvent e) {
				TeacherService  service = new TeacherServiceImpl();
				//获取文本框的内容 
				String classes = textField.getText().trim();
				if(classes.equals("")) {
					JOptionPane.showMessageDialog(null, "请输入课程代码", "提示", JOptionPane.CLOSED_OPTION);
					return;
				}
				//用课程代码导入数据
				studentList = service.findStudentAll(conn,classes); 
				studentname.setText(studentList.get(0).getStudentName());
				index = 0;
			}
		});
		button.setFont(new Font("宋体", Font.BOLD, 20));
		
		studentname = new JLabel("显示学生姓名");
		studentname.setFont(new Font("宋体", Font.BOLD, 40));
		
		JButton btnNewButton = new JButton("正常上课");
		/***
		 * 单击正常上课单击事件
		 * 
		 */
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(index!=studentList.size()) {
					studentname.setText(studentList.get(index).getStudentName());
					++index;
				}else {
					JOptionPane.showMessageDialog(null, "点名结束", "提示", JOptionPane.CLOSED_OPTION);
					return;
				}
			}
		});
		btnNewButton.setFont(new Font("宋体", Font.BOLD, 20));
		
		JButton button_1 = new JButton("旷课");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				CallRollService service = new CallRollServiceImpl();
				
				 
				if(index!=studentList.size()) {
					
					int spointOut = service.updateCallRollByStudentName(conn,studentList.get(index),teacher.getCourseNum());
					
					if(spointOut==1) {
						JOptionPane.showMessageDialog(null , "更新成功", "提示", JOptionPane.CLOSED_OPTION);
					}
					
				}else {
					JOptionPane.showMessageDialog(null, "点名结束", "提示", JOptionPane.CLOSED_OPTION);
					return;
				}
				studentname.setText(studentList.get(index).getStudentName());
				++index;
			}
		});
		button_1.setFont(new Font("宋体", Font.BOLD, 20));
		
		JButton button_2 = new JButton("请假");
		button_2.setFont(new Font("宋体", Font.BOLD, 20));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
					.addContainerGap(260, Short.MAX_VALUE)
					.addComponent(studentname, GroupLayout.PREFERRED_SIZE, 366, GroupLayout.PREFERRED_SIZE)
					.addGap(200))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(32)
					.addComponent(label)
					.addGap(30)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(32)
							.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)
							.addGap(50)
							.addComponent(button_1)
							.addGap(53)
							.addComponent(button_2))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(textField, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)
							.addGap(28)
							.addComponent(button)))
					.addContainerGap(259, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(43)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
						.addComponent(label)
						.addComponent(button))
					.addGap(97)
					.addComponent(studentname, GroupLayout.PREFERRED_SIZE, 118, GroupLayout.PREFERRED_SIZE)
					.addGap(88)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(button_2)
						.addComponent(button_1)
						.addComponent(btnNewButton))
					.addContainerGap(99, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);

	}

}
